package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.DefaultEnum;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EAppletsJumpType;
import com.gzhryc.shared_device.common.payment.dao.db.PayAccountGroup;
import com.gzhryc.shared_device.common.payment.dao.models.CashWithdrawalConfig;
import com.gzhryc.shared_device.common.payment.services.PayAccountGroupService;
import com.gzhryc.shared_device.common.payment.services.dto.PayAccountGroupSearch;
import com.gzhryc.shared_device.oem.code.profit.service.ProjectWalletService;
import com.gzhryc.shared_device.oem.h5.mng.DataTools;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class PayAccountGroupController {

    @Route(value = { "/config/account_group/list.html" })
    public String list(HttpServletRequest request) {
        return "/config/account_group/list.html";
    }

    @Route(value = "/config/account_group/list.data")
    public ListResult<PayAccountGroup> listData(HttpServletRequest request) {
        ListResult<PayAccountGroup> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PayAccountGroupSearch search = WebUtils.getParameter(request,PayAccountGroupSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(PayAccountGroupService.self().countBySearch(search));
        List<PayAccountGroup> accountGroupList = PayAccountGroupService.self().findBySearch(search,pageNo,pageLimit);
        if(accountGroupList != null && accountGroupList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(accountGroupList);
        }
        return result;
    }

    @Route(value = { "/config/account_group/add.html" })
    public String add(HttpServletRequest request) {
        return "/config/account_group/edit.html";
    }

    @Route(value = {"/config/account_group/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        PayAccountGroup payAccountGroup = WebUtils.getParameter(request,PayAccountGroup.class);
        try {
            if(PayAccountGroupService.self().add(payAccountGroup,ProjectWalletService.self())){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/config/account_group/info.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        PayAccountGroup payAccountGroup = PayAccountGroupService.self().getById(id);
        if(payAccountGroup != null) {
            WebUtils.setAttribute(request, payAccountGroup);

            if(StringTools.isNotBlank(payAccountGroup.getTransferConfig())){
                CashWithdrawalConfig config = JsonTools.fromJson(payAccountGroup.getTransferConfig(),CashWithdrawalConfig.class);
                request.setAttribute("config",config);
            }else{
                request.setAttribute("config",new CashWithdrawalConfig());
            }

            if(StringTools.isNotBlank(payAccountGroup.getMemberTransferConfig())){
                CashWithdrawalConfig config = JsonTools.fromJson(payAccountGroup.getMemberTransferConfig(),CashWithdrawalConfig.class);
                request.setAttribute("memberConfig",config);
            }else{
                request.setAttribute("memberConfig",new CashWithdrawalConfig());
            }
        }
        request.setAttribute("appletsJumpTypeList", EAppletsJumpType.values());
        return "/config/account_group/info.html";
    }

    @Route(value = {"/config/account_group/update.do"})
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败，请稍后再试");
        PayAccountGroup payAccountGroup = WebUtils.getParameter(request,PayAccountGroup.class);
        try {
            String dayTotalMoneyStr = WebUtils.getString(request, "dayTotalMoneyStr");
            if(StringTools.isNotBlank(dayTotalMoneyStr)){
                payAccountGroup.setDayTotalMoney(NumberTools.changeMoney(Double.valueOf(dayTotalMoneyStr)));
            }else{
                throw new LogicException("请输入代理每天总提现金额");
            }

            String memberDayTotalMoneyStr = WebUtils.getString(request, "memberDayTotalMoneyStr");
            if(StringTools.isNotBlank(memberDayTotalMoneyStr)){
                payAccountGroup.setMemberDayTotalMoney(NumberTools.changeMoney(Double.valueOf(memberDayTotalMoneyStr)));
            }else{
                throw new LogicException("请输入会员每天总提现金额");
            }

            CashWithdrawalConfig config = DataTools.getCashWithdrawalConfig(request,"config");
            String cashWithdrawalWeeks = WebUtils.getString(request,"cashWithdrawalWeeks");
            config.setCashWithdrawalWeeks(cashWithdrawalWeeks);
            payAccountGroup.setTransferConfig(JsonTools.toJson(config));

            CashWithdrawalConfig memberConfig = DataTools.getCashWithdrawalConfig(request,"memberConfig");
            String memberCashWithdrawalWeeks = WebUtils.getString(request,"memberCashWithdrawalWeeks");
            memberConfig.setCashWithdrawalWeeks(memberCashWithdrawalWeeks);
            payAccountGroup.setMemberTransferConfig(JsonTools.toJson(memberConfig));

            if(payAccountGroup.getCanTransfer() == null){
                payAccountGroup.setCanTransfer(false);
            }

            if(payAccountGroup.getWxTransfer() == null){
                payAccountGroup.setWxTransfer(false);
            }

            if(payAccountGroup.getMemberCanTransfer() == null){
                payAccountGroup.setMemberCanTransfer(false);
            }

            if(PayAccountGroupService.self().updateNotNull(payAccountGroup)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = {"/config/account_group/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        Long id = WebUtils.getLong(request,"id");
        try {
            if(PayAccountGroupService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route("/config/account_group/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);

        PayAccountGroupSearch search = new PayAccountGroupSearch();
        search.setName(term);
        Long total = PayAccountGroupService.self().countBySearch(search);
        List<PayAccountGroup> list = PayAccountGroupService.self().findBySearch(search, page, WebConstants.PAGE_LIMIT);
        if (list != null && list.size() > 0) {
            for (PayAccountGroup accountGroup : list) {
                SelectResult.Item item = new SelectResult.Item();
                item.setId(accountGroup.getId().toString());
                item.setText(accountGroup.getName());
                result.getBody().getData().add(item);
            }
            total += list.size();
        }
        result.getBody().setPage(page);
        result.getBody().setLimit(WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(total);
        return result;
    }

    @Route(value = { "/config/cash_withdrawal/weeks.data" })
    public SelectResult weeks(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        SelectResult result = new SelectResult(DtoConstants.EXECUTE_ERROR, "获取失败");
        DefaultEnum.Weeks[] weeks = DefaultEnum.Weeks.values();
        for (int i = 0; i < weeks.length; i++) {
            DefaultEnum.Weeks week = weeks[i];
            SelectResult.Item item = new SelectResult.Item();
            item.setId(week.name());
            item.setText(week.note());
            result.getBody().getData().add(item);
        }
        result.setCode(DtoConstants.SUCCESS, null);
        return result;
    }
}
